<?php

namespace Database\Seeders;

use App\Models\Dish;
use App\Models\Category;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class SystemDishSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 清空菜品表（使用delete而不是truncate避免外键约束问题）
        DB::table('dishes')->delete();

        // 获取分类
        $categories = Category::all()->keyBy('slug');
        
        $dishes = [
            // 热菜
            [
                'name' => '宫保鸡丁',
                'description' => '经典川菜，鸡肉丁配花生米，麻辣鲜香',
                'category_id' => $categories['hot']->id,
                'creator_id' => null,
                'menu_id' => null,
                'source_dish_id' => null,
                'dish_source' => 'system',
                'is_public' => true,
                'image' => 'https://picsum.photos/400/300?random=1',
                'tags' => json_encode(['川菜', '经典', '麻辣']),
                'flavors' => json_encode(['麻辣', '微辣', '不辣']),
                'cooking_time' => 15,
                'is_available' => true,
                'is_featured' => true,
                'sort_order' => 1,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'name' => '红烧肉',
                'description' => '传统家常菜，肥瘦相间，香甜软糯',
                'category_id' => $categories['hot']->id,
                'creator_id' => null,
                'menu_id' => null,
                'source_dish_id' => null,
                'dish_source' => 'system',
                'is_public' => true,
                'image' => 'https://picsum.photos/400/300?random=2',
                'tags' => json_encode(['家常菜', '经典', '香甜']),
                'flavors' => json_encode(['香甜', '咸甜']),
                'cooking_time' => 45,
                'is_available' => true,
                'is_featured' => true,
                'sort_order' => 2,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'name' => '麻婆豆腐',
                'description' => '四川名菜，豆腐嫩滑，麻辣鲜香',
                'category_id' => $categories['hot']->id,
                'creator_id' => null,
                'menu_id' => null,
                'source_dish_id' => null,
                'dish_source' => 'system',
                'is_public' => true,
                'image' => 'https://picsum.photos/400/300?random=3',
                'tags' => json_encode(['川菜', '麻辣', '素食']),
                'flavors' => json_encode(['麻辣', '微辣', '不辣']),
                'cooking_time' => 10,
                'is_available' => true,
                'is_featured' => false,
                'sort_order' => 3,
                'view_count' => rand(100, 1000),
                'order_count' => rand(50, 500),
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ];

        Dish::insert($dishes);
        
        $this->command->info('已创建 ' . count($dishes) . ' 个系统菜品数据');
    }
}